package com.zrx.springboot.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrx.springboot.entity.Admin;
import com.zrx.springboot.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;

@Controller
public class adminController {
    @Autowired
    private AdminService adminService;

    @GetMapping("/admin_table")
    public String mainPage(HttpSession session) {

        return "form/admin_table";
    }

    /**
     * 获取用户信息
     */
    @RequestMapping("/getadminData")
    public String getUserData(Model model, Integer page, Integer size){
        System.out.println("显示图书信息------------");
        //mybatis puls分页
        IPage<Admin> iPage = adminService.page(new Page<>(page,size));
        model.addAttribute("iPage", iPage);
        model.addAttribute("pre", iPage.getCurrent()-1);
        model.addAttribute("next", iPage.getCurrent()+1);
        model.addAttribute("cur", iPage.getCurrent());//当前页
        model.addAttribute("last", iPage.getPages());//总共页

        model.addAttribute("size",size);
        return "form/adminDate";
    }


    /**
     * 根据名字模糊查询获取信息
     */
    @RequestMapping("/getUserNameData")
    public String getUserNameData(Model model, Integer page,Integer size,String adminname){
        System.out.println("显示用户信息------------");
        System.out.println(adminname);
        //mybatis puls分页

        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("adminname",adminname);
        IPage<Admin> iPage = adminService.page(new Page<>(page,size),queryWrapper);

        model.addAttribute("iPage", iPage);
        model.addAttribute("pre", iPage.getCurrent()-1);
        model.addAttribute("next", iPage.getCurrent()+1);
        model.addAttribute("cur", iPage.getCurrent());//当前页
        model.addAttribute("last", iPage.getPages());//总共页
        model.addAttribute("size",size);
        return "form/adminDate";
    }

    /**
     * 退出
     *
     */
    @RequestMapping("/out")
    public String out(HttpSession session){
        Integer id = (Integer) session.getAttribute("id");
        System.out.println("-------");
        System.out.println(id);
        java.util.Date date1 = new java.util.Date();

        Timestamp date= new Timestamp(date1.getTime());
        adminService.update(id,date);
        //删除
        session.removeAttribute("aminId");
        session.removeAttribute("loginUser");

        return "login";
    }
    /**
     * 修改密码
     */
    @RequestMapping("/upadmin")
    public String upadmin(HttpSession session,String password){
        Integer id = (Integer) session.getAttribute("id");
        System.out.println("-------");
        System.out.println(id);
        adminService.upword(id,password);

        return "index";
    }

}
